function sort(arr) {
  if (arr.length <= 1) return arr

  let piovtIndex = Math.floor(arr.length / 2)
  let piovt = arr.splice(piovtIndex, 1)[0]
  let left = []
  let right = []
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] <= piovt) {
      right.push(arr[i])
    } else {
      left.push(arr[i])
    }
  }
  return sort(left).concat([piovt], sort(right))
}
console.log(sort([1, 2, 4, 5, 3, 7, 4]))